package com.bw.educate.util;


import com.bw.educate.common.auth.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
@Component
public class UserUtil {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public UserInfo getLoginUser(HttpServletRequest request){
        //获取cookie中的token值
        Cookie[] cookies = request.getCookies();
        String token="";
        for (Cookie cookie: cookies) {
            if(cookie.getName().equals("token")){
                token=cookie.getValue();
            }
        }
        //根据token获取redis中的用户信息
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(token);
        return userInfo;
    }
}
